package casts.misc
{
   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.geom.Point;
   import flash.text.TextField;
   // Progression
   import jp.progression.casts.CastMovieClip;
   // GreenSock
   import com.greensock.TweenMax;

   /**
    * 故名思義就是 mouse over 後會秀出一個 information popup 的範本。
    * @author cjboy1984@gmail.com
    * @usage
    * import jp.progression.casts.getInstanceById;
    * import casts.misc.RolloverInfo;
    *
    * // getter
    * public static function get rollover_info():MovieClip { return getInstanceById('RobotOver') as MovieClip; }
    *
    * rollover_info.showInfo( ... );
    * rollover_info.hideInfo();
    */
   public class RolloverInfo extends CastMovieClip
   {
      // fla
      public var tfNo:TextField;
      public var tfVotes:TextField;
      public var tfName:TextField;

      public function RolloverInfo(initObject:Object = null)
      {
         mouseEnabled = false;
         mouseChildren = false;
         buttonMode = false;

         super(initObject);

         stop();
      }

      // --------------------- LINE ---------------------

      // 顯示資訊
      public function showInfo(_name:String, _no:String, _votes:String):void
      {
         tfNo.text = _no;
         tfVotes.text = _votes;
         tfName.text = _name;

         visible = true;
         TweenMax.killTweensOf(this);
         TweenMax.to(this, 0.3, { alpha:1 } );
      }

      // 隱藏資訊
      public function hideInfo():void
      {
         TweenMax.killTweensOf(this);
         TweenMax.to(this, 0.3, { alpha:0, delay:0.3, visible:false } );
      }

      // --------------------- LINE ---------------------

      // ################### protected ##################

      override protected function atCastAdded():void
      {
         alpha = 0;
         visible = false;
         stage.addEventListener(MouseEvent.MOUSE_MOVE, followMouse);
      }

      override protected function atCastRemoved():void
      {
         stage.removeEventListener(MouseEvent.MOUSE_MOVE, followMouse);
      }

      // #################### private ###################

      private function followMouse(e:Event):void
      {
         var new_x:Number = stage.mouseX;
         var new_y:Number = stage.mouseY;

         // 偵測當滑鼠太靠近右邊邊界時，選單會往左擠
         if (new_x + width > stage.stageWidth) new_x = stage.mouseX - 34 - width;
         TweenMax.to(this, 0.3, { x:new_x, y:new_y } );
      }

      // --------------------- LINE ---------------------

   }

}
